﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    public class LGC_VAL_BOM_SUB2
    {
        public static DataTable LGC_VAL_BOM_SUB2_VALUE(DataTable dt_BOM, string DWG_No, string Part_No, string Process_Code, string WORKTIME_LOGIC_CODE, string feature_List)
        {
            //"子程序2，参数：PXX_BOM特征
            //a、遍历抽出BOM数据中的[BOM].[COMPONENT]
            //b、返回逻辑
            //IF [BOM].[COMPONENT]物料出现在[IE_BOM_Comp_MatCheck].[CHECK_MAT]
            //     返回[Operation_Name]
            //     IF [Operation_Name]包含在【PXX_BOM特征】
            //         返回[Operation_Name]对应特征累加，即[PXX_...]=[PXX_...]+[BOM].[SINGLE_QTY]
            //   END IF
            //END IF"

            //定义临时表接收特征结果值
            DataTable dt = Common.GetTable.GetTable_SUB2();

            string[] list = feature_List.Split(',');
            DataTable dtFeature_List = Common.GetTable.GetTable_Feature();

            foreach (var item in list)
            {
                for (int i = 0; i < dt_BOM.Rows.Count; i++)
                {
                    DataTable dtTemp = BaseBuildSql.GetDataTableBySql("select FEATURE_NAME from IE_BOM_Comp_MatCheck where CHECK_MAT='" + dt_BOM.Rows[i]["COMPONENT"].ToString() + "' AND FEATURE_NAME='" + item + "'");
                    if (dtTemp.Rows.Count > 0)
                    {

                        //foreach (var item in list)
                        //{
                        if (item == dtTemp.Rows[0]["FEATURE_NAME"].ToString())
                        {

                            double FEATURE_VALUES = dt_BOM.Rows[i]["SINGLE_QTY"].ToString().Length > 0 ?
                           Convert.ToDouble(dt_BOM.Rows[i]["SINGLE_QTY"].ToString()) : 0;
                            DataRow dr = dt.NewRow();
                            dr["PART_NO"] = Part_No;
                            dr["PROCESS_CODE"] = Process_Code;
                            dr["FEATURE_NAME"] = dtTemp.Rows[0]["FEATURE_NAME"].ToString();
                            dr["FEAT_VALUE"] = FEATURE_VALUES;
                            dt.Rows.Add(dr);

                            //double FEATURE_VALUES = dt_BOM.Rows[i]["SINGLE_QTY"].ToString().Length > 0 ?
                            //Convert.ToDouble(dt_BOM.Rows[i]["SINGLE_QTY"].ToString()) : 0;
                            //DataRow dr = dtFeature_List.NewRow();
                            //dr["DWG_NO"] = DWG_No;
                            //dr["PART_NO"] = Part_No;
                            //dr["PROCESS_CODE"] = Process_Code;
                            //dr["FEATURE_NAME"] = dtTemp.Rows[0]["FEATURE_NAME"].ToString();
                            //dr["FEAT_VALUE"] = FEATURE_VALUES;
                            //dr["LABOR_VALUE"] = Calculate.LGC_TIM_VTA.LGC_TIM_VTA_VALUE(DWG_No, Part_No, dtTemp.Rows[0]["FEATURE_NAME"].ToString(), "", FEATURE_VALUES);//根据特征定义表工时字段计算
                            //dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff");
                            //dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                            //dtFeature_List.Rows.Add(dr);

                        }
                    }
                }
            }
            DataTable dtResult = dt.Clone();
            DataTable dtName = dt.DefaultView.ToTable(true, "PART_NO", "PROCESS_CODE", "FEATURE_NAME");//
            for (int i = 0; i < dtName.Rows.Count; i++)
            {
                DataRow[] rows = dt.Select("FEATURE_NAME='" + dtName.Rows[i]["FEATURE_NAME"] + "'");//
                DataTable temp = dtResult.Clone();
                foreach (DataRow row in rows)
                {
                    temp.Rows.Add(row.ItemArray);
                }
                DataRow dr = dtResult.NewRow();
                dr[0] = dtName.Rows[i]["PART_NO"].ToString();
                dr[1] = dtName.Rows[i]["PROCESS_CODE"].ToString();
                dr[2] = dtName.Rows[i]["FEATURE_NAME"].ToString();
                dr[3] = temp.Compute("sum(FEAT_VALUE)", "");//compute 是一个函数用于计算，只有两个参数
                dtResult.Rows.Add(dr);
            }

            for (int j = 0; j < dtResult.Rows.Count; j++)
            {
                if (Convert.ToDouble(dtResult.Rows[j]["FEAT_VALUE"].ToString()) > 0)
                {
                    DataRow dr = dtFeature_List.NewRow();
                    dr["DWG_NO"] = DWG_No;
                    dr["PART_NO"] = Part_No;
                    dr["PROCESS_CODE"] = Process_Code;
                    dr["FEATURE_NAME"] = dtResult.Rows[j]["FEATURE_NAME"].ToString();
                    dr["FEAT_VALUE"] = dtResult.Rows[j]["FEAT_VALUE"].ToString(); ;
                    dr["LABOR_VALUE"] = Calculate.LGC_TIM_VTA.LGC_TIM_VTA_VALUE(DWG_No, Part_No, dtResult.Rows[j]["FEATURE_NAME"].ToString(), "", Convert.ToDouble(dr["FEAT_VALUE"]));//根据特征定义表工时字段计算
                    dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                    dtFeature_List.Rows.Add(dr);
                }
            }

            return dtFeature_List;
        }
    }
}
